import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { PlatformLocation } from '@angular/common';
import { NbToastrService, NbDialogService } from '@nebular/theme';
import { UserService } from '@nx-chat/common/service';
import { UserDto } from '@nx-chat/common/dto';
import { SearchFriendComponent } from '@nx-chat/common/components';

@Component({
  selector: 'friends-list',
  templateUrl: './friends-list.component.html',
  styleUrls: ['./friends-list.component.scss']
})
export class FriendsListComponent implements OnInit {

  friendList: UserDto[];

  groupList = [];

  constructor(
    private _userService: UserService,
    private _router: Router,
    private _activateRoute: ActivatedRoute,
    private platform: PlatformLocation,
    private toasterService: NbToastrService,
    private dialogService: NbDialogService,
  ) {}

  /**
   * @function 触发加好友弹框
   */
  public searchFriend(): void {
    const ref = this.dialogService.open(SearchFriendComponent);
    // ref.onClose.pipe(take(1)).subscribe((res) => {if (res) this.loadRoleList()});
  }

  public isActive(email: string): boolean {
    const url = this.platform.pathname.substr(1);
    return url.includes(email);
  }

  public getFriendInfo(email: string) {
    this._router.navigate([`friend/${email}`]);
  }

  ngOnInit() {
    const uid = this._userService.uId;
    this._userService.getFriendsList(uid).subscribe(data => {
      this.friendList = data;
    })
  }

}
